<?php

namespace app\admin\controller\mall;

use app\admin\model\MallOrder;
use app\admin\model\MallTradeRecordLog;
use app\admin\model\MallUser;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="推广订单")
 */
class PromotionOrder extends AdminController
{
    
    
    public function __construct(App $app)
    {
        parent::__construct($app);
        
        $this->model = new \app\admin\model\MallPromotion();
        
    }
    
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            [$page, $limit, $where] = $this->buildTableParames();
            $where[] = ['trade_type', '=', 17];
            $count   = MallTradeRecordLog::withJoin(['mallOrder', 'promotion', 'orderUser'], 'left')
                ->where($where)
                ->count();
            $list    = MallTradeRecordLog::withJoin(['mallOrder', 'promotion', 'orderUser'], 'left')
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select()->toArray();
            foreach ($list as &$value) {
                $value[ 'amount' ]                  = getPrice($value[ 'amount' ]);
                $value[ 'mallOrder' ][ 'pay_time' ] = date('Y-m-d H:i:s', $value[ 'mallOrder' ][ 'pay_time' ]);
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
}